/** index.js **/
//获取app实例
const app = getApp();
Page({
    data: {
        userInfo: {},   // 用户信息
        hasLogin: wx.getStorageSync('loginFlag')
            ? true 
            : false     // 是否登录，根据后台返回的skey判断
    },
    // 检查本地 storage 中是否有skey登录态标识
    checkLoginStatus: function() {
        let that = this;
        let loginFlag = wx.getStorageSync('loginFlag');
        if (loginFlag) {
            // 检查 session_key 是否过期
            wx.checkSession({
                // session_key 有效(未过期)
                success: function() {
                    // 获取用户头像/昵称等信息
                let userInfo= wx.getStorageSync('userInfo');
                console.log(userInfo);
                 that.setData({userInfo:userInfo});
                },
                // session_key 已过期
                fail: function() {
                    that.setData({
                        hasLogin: false
                    });
                }
            });
        } else {
            that.setData({
                hasLogin: false
            });
        }
    },
   
    /**
     * 跳转已购书籍页面
     */
    goMyFrance: function() {
        wx.navigateTo({
            url: '../france/france'
        });
    },
    /**
     * 从 globalData 中获取 userInfo
     */
    getUserProfile: function() {
        let that = this;
        wx.getUserProfile({desc:'sdsd',success:function(rel){
            app.doLogin(rel,function(){
                let userInfo = wx.getStorageSync('userInfo');
                console.info('userInfo is:', userInfo);
                if (userInfo) {
                    that.setData({
                        hasLogin: true,
                        userInfo: userInfo
                    });
                    wx.hideLoading();
                } else {
                    console.log('globalData中userInfo为空');
                }
            });
        },fail:function(e){ console.log(e); }});
    },
    onLoad: function() {
        this.checkLoginStatus();//检测是否登陆
    },
    onShow: function() {
        let that = this;
        that.setData({
            userInfo:  wx.getStorageSync('userInfo')
        });
    },
    enterCommunity:function(){
        let userInfo=wx.getStorageSync('userInfo');
        app.post('bind&op=isbind',{memberid:userInfo.id},function(rel){
                let data=rel.data;
                if(data.isbind){
                    wx.navigateTo({
                        url: '../info/info',
                      })
                }else{
                    wx.navigateTo({
                        url: '../enterCommunity/enterCommunity',
                      })
                }
        });
     
    }
})